package com.yenlien.traceyun.business.service;

import com.yenlien.traceyun.business.dao.ProductDao;
import com.yenlien.traceyun.business.entity.Product;
import com.yenlien.traceyun.business.service.rest.usercenter.FileRestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.persistence.criteria.*;
import java.util.*;


/**
 * Created by LiuSha on 2020/7/1.
 */
@Service
public class ProductService {
    @Autowired
    private ProductDao productDao;
    @Autowired
    private FileRestService fileRestService;

    public Page<Product> findByList(final Product product) {
        Pageable pageable = PageRequest.of(product.getPage(), product.getPageSize(), Sort.Direction.DESC, "createtime");
        return productDao.findAll(new Specification<Product>() {
            private static final long serialVersionUID = 1L;

            @Override
            public Predicate toPredicate(Root<Product> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Path<String> companyId = root.get("companyId");
                //构造查询条件
                List<Predicate> list = new ArrayList<>();
                List<Predicate> or = new ArrayList<>();
                if (!StringUtils.isEmpty(product.getCompanyId())) {
                    list.add(cb.equal(companyId, product.getCompanyId()));
                }
                Predicate[] predicate = new Predicate[list.size()];
                Predicate pre_and = cb.and(list.toArray(predicate));
                if (!StringUtils.isEmpty(product.getQuery())) {
                    or.add(cb.like(root.get("title"), "%" + product.getQuery() + "%"));
                    or.add(cb.like(root.get("relatedProducts"), "%" + product.getQuery() + "%"));
                } else {
                    return pre_and;
                }
                Predicate[] pre_or = new Predicate[or.size()];
                Predicate ora = cb.or(or.toArray(pre_or));
                return query.where(pre_and, ora).getRestriction();
            }
        }, pageable);
    }

    public void save(Product product, List<MultipartFile> files) throws Exception {
        fileRestService.upload(null, product, files);
        productDao.save(product);
    }

    public void save2(Product product) throws Exception {
        productDao.save(product);
    }

//    public void save(Product product) throws Exception {
//        productDao.save(product);
//    }


    public void update(Product product, List<MultipartFile> files) throws Exception {
        fileRestService.upload(productDao.findById(product.getId()), product, files);
        productDao.save(product);
    }


    public Product findById(String id) {
        Product product = productDao.findById(id);
        // 算出还有多久过期
        Date createtime = product.getCreatetime();
        Calendar cal = Calendar.getInstance();
        cal.setTime(createtime);
        cal.add(Calendar.MONTH, Integer.parseInt(product.getQuality()));
        // 两个日期相差的天数
        Integer days = (int) ((cal.getTime().getTime() - new Date().getTime()) / (1000 * 3600 * 24));
        product.setOutDate(days.toString());
        return product;
    }

    @Transactional
    public void delete(String id) {
        productDao.deleteById(id);
    }

    public List<Product> findAll(String companyId) {
        if (!StringUtils.isEmpty(companyId)) {
            return productDao.findByCompanyId(companyId);
        }
        return productDao.findAll();
    }

}
